package com.zoke.dappstore.adapter;

import android.support.annotation.Nullable;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.inf.IEntity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.zoke.dappstore.R;
import com.zoke.dappstore.model.Dapp;
import com.zoke.dappstore.tools.Tools;

import org.xutils.common.util.LogUtil;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

/**
 * Created by wulijie on 2018/6/11.
 */
public class DmAdapter extends BaseQuickAdapter<DownloadEntity, BaseViewHolder> {
    public DmAdapter(@Nullable List<DownloadEntity> data) {
        super(R.layout.item_dm, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, DownloadEntity item) {
        ProgressBar progress = helper.getView(R.id.progress);
        ImageView imageView = helper.getView(R.id.headIv);
        TextView tv_state = helper.getView(R.id.tv_size);
        TextView tv_btn = helper.getView(R.id.tv_btn);
        TextView tv_zh = helper.getView(R.id.tv_zh);
        String json = item.getStr();
        Dapp dapp = new Gson().fromJson(json, Dapp.class);
        ImageOptions options = new ImageOptions.Builder()
                .setRadius(20)
                .build();
        x.image().bind(imageView, dapp.icon, options);
        helper.setText(R.id.tv_title, dapp.name);
        String str = "";
        String state = "";
        int per = item.getPercent();
        float pp = per / 100f;
        float cur = pp * Long.parseLong(dapp.size);
        long curr = (long) cur;
        switch (item.getState()) {
            case IEntity.STATE_WAIT:
            case IEntity.STATE_OTHER:
            case IEntity.STATE_FAIL:
                str = "开始";
                state = "下载失败";
                progress.setProgress(per);
                tv_zh.setText(Tools.formatSize(curr + "") + "/" + Tools.formatSize(dapp.size));
                break;
            case IEntity.STATE_STOP:
                str = "恢复";
                state = "暂停中";
                progress.setProgress(per);
                tv_zh.setText(Tools.formatSize(curr + "") + "/" + Tools.formatSize(dapp.size));
                break;
            case IEntity.STATE_PRE:
            case IEntity.STATE_POST_PRE:
            case IEntity.STATE_RUNNING:
                str = "下载中";
                state = "下载中";
                progress.setProgress(per);
                LogUtil.e("po = " + progress);
                tv_zh.setText(Tools.formatSize(curr + "") + "/" + Tools.formatSize(dapp.size));
                break;
            case IEntity.STATE_COMPLETE:
                str = "安装";
                state = "下载完";
                progress.setProgress(100);
                tv_zh.setText(Tools.formatSize(dapp.size) + "/" + Tools.formatSize(dapp.size));
                break;
        }
        tv_state.setText(state);
        tv_btn.setText(str);
        tv_btn.setOnClickListener(v -> {
            switch (item.getState()) {
                case IEntity.STATE_WAIT:
                case IEntity.STATE_OTHER:
                case IEntity.STATE_FAIL:
                case IEntity.STATE_STOP:
                case IEntity.STATE_PRE:
                case IEntity.STATE_POST_PRE:
                    Aria.download(mContext).load(item).start();
                    break;
                case IEntity.STATE_RUNNING:
//                    Aria.download(mContext).load(item).stop();
                    break;
                case IEntity.STATE_COMPLETE:
                    LogUtil.e("任务已完成");
                    //安装
                    String downloadPath = item.getDownloadPath();
                    Tools.install(mContext, downloadPath);
                    break;
            }
        });
    }

    public synchronized void setDone(DownloadEntity entity) {
        for (int x = 0; x < mData.size(); x++) {
            DownloadEntity de = mData.get(x);
            if (de.getDownloadPath().equals(entity.getDownloadPath())) {
                mData.set(x, entity);
                notifyItemChanged(x, entity);
                break;
            }
        }
    }


    /**
     * 更新进度
     */
    public synchronized void setProgress(DownloadEntity entity) {
        LogUtil.e("setProgress = " + entity.getPercent());
        LogUtil.e("getDownloadPath = " + entity.getDownloadPath());
        for (int x = 0; x < mData.size(); x++) {
            DownloadEntity de = mData.get(x);
            if (de.getDownloadPath().equals(entity.getDownloadPath())) {
                mData.set(x, entity);
                notifyItemChanged(x, entity);
                break;
            }
        }
    }
}
